package md.mru.frontend.controllers;

import md.mru.core.dtos.*;
import md.mru.core.service.SkillsDataService;
import org.primefaces.context.RequestContext;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sergiu.marin
 * @since 29.03.2014
 */
@ManagedBean(name = "skillsDataBean")
@ViewScoped
public class SkillsDataAdministrationBean extends BackingBean {

    private static final long serialVersionUID = -8689440254784999383L;

    @ManagedProperty(value = "#{skillsDataService}")
    private SkillsDataService skillsDataService;
    @ManagedProperty(value = "#{folderAdministrationBean}")
    private FolderAdministrationBean folderAdministrationBean;

    private List<EducationDto> educations = new ArrayList<>();
    private List<EducationTypeDto> educationTypes = new ArrayList<>();
    private List<EducationDomainDto> educationDomains = new ArrayList<>();
    private List<ModernLanguageDto> modernLanguages = new ArrayList<>();
    private List<LanguageDto> languages = new ArrayList<>();
    private List<KnowledgeLevelDto> knowledgeLevels = new ArrayList<>();
    private EducationDto selectedEducation;
    private ModernLanguageDto selectedModernLanguage;

    public void resetCreateLanguageDialog() {
        this.selectedModernLanguage = new ModernLanguageDto();
        this.selectedModernLanguage.setFolder(folderAdministrationBean.getSelectedFolder().getId());
        this.initLanguageDialog();
    }

    public void initEditLanguageDialog(ModernLanguageDto modernLanguage) {
        this.selectedModernLanguage = modernLanguage;
        this.initLanguageDialog();
    }

    public void initLanguageDialog() {
        if (languages.isEmpty()) {
            languages = skillsDataService.getLanguages();
            this.getObjectMappingConverter().mapObjectList(languages);
        }
        if (knowledgeLevels.isEmpty()) {
            knowledgeLevels = skillsDataService.getKnowledgeLevels();
            this.getObjectMappingConverter().mapObjectList(knowledgeLevels);
        }
    }

    public void resetCreateEducationDialog() {
        this.selectedEducation = new EducationDto();
        this.selectedEducation.setFolder(folderAdministrationBean.getSelectedFolder().getId());
        this.initEducationDialog();
    }

    public void initEditEducationDialog(EducationDto education) {
        this.selectedEducation = education;
        this.initEducationDialog();
    }

    public void initEducationDialog() {
        if (educationTypes.isEmpty()) {
            educationTypes = skillsDataService.getEducationTypes();
            this.getObjectMappingConverter().mapObjectList(educationTypes);
        }
        if (educationDomains.isEmpty()) {
            educationDomains = skillsDataService.getEducationDomains();
            this.getObjectMappingConverter().mapObjectList(educationDomains);
        }
    }

    public void initSkillsDataTab() {
        this.educations = skillsDataService.getEducations(this.folderAdministrationBean.getSelectedFolder().getId());
        this.modernLanguages = skillsDataService.getModernLanguages(this.folderAdministrationBean.getSelectedFolder().getId());
    }

    public void saveEducation() {
        long id = skillsDataService.saveEducation(selectedEducation);
        if (selectedEducation.getId() == 0) {
            selectedEducation.setId(id);
            educations.add(selectedEducation);
        }
    }

    public void deleteEducation(EducationDto e) {
        skillsDataService.deleteEducation(e.getId());
        this.educations.remove(e);
    }

    public void saveModernLanguage() {
        long id = skillsDataService.saveModernLanguage(selectedModernLanguage);
        if (selectedModernLanguage.getId() == 0) {
            selectedModernLanguage.setId(id);
            modernLanguages.add(selectedModernLanguage);
        }
    }

    public void deleteModernLanguage(ModernLanguageDto ml) {
        skillsDataService.deleteModernLanguage(ml.getId());
        this.modernLanguages.remove(ml);
    }

    public void addNewModernLanguage(String editRowId) {
        ModernLanguageDto ml = new ModernLanguageDto();
        modernLanguages.add(ml);
        RequestContext.getCurrentInstance().execute(this.constructOnCompleteScript("ml-formId", "mlData", editRowId));
    }

    public List<EducationDto> getEducations() {
        return educations;
    }

    public void setEducations(List<EducationDto> educations) {
        this.educations = educations;
    }

    public List<EducationDomainDto> getEducationDomains() {
        return educationDomains;
    }

    public void setEducationDomains(List<EducationDomainDto> educationDomains) {
        this.educationDomains = educationDomains;
    }

    public List<EducationTypeDto> getEducationTypes() {
        return educationTypes;
    }

    public void setEducationTypes(List<EducationTypeDto> educationTypes) {
        this.educationTypes = educationTypes;
    }

    public List<ModernLanguageDto> getModernLanguages() {
        return modernLanguages;
    }

    public void setModernLanguages(List<ModernLanguageDto> modernLanguages) {
        this.modernLanguages = modernLanguages;
    }

    public List<LanguageDto> getLanguages() {
        return languages;
    }

    public void setLanguages(List<LanguageDto> languages) {
        this.languages = languages;
    }

    public List<KnowledgeLevelDto> getKnowledgeLevels() {
        return knowledgeLevels;
    }

    public void setKnowledgeLevels(List<KnowledgeLevelDto> knowledgeLevels) {
        this.knowledgeLevels = knowledgeLevels;
    }

    public EducationDto getSelectedEducation() {
        return selectedEducation;
    }

    public void setSelectedEducation(EducationDto selectedEducation) {
        this.selectedEducation = selectedEducation;
    }

    public void setSkillsDataService(SkillsDataService skillsDataService) {
        this.skillsDataService = skillsDataService;
    }

    public ModernLanguageDto getSelectedModernLanguage() {
        return selectedModernLanguage;
    }

    public void setSelectedModernLanguage(ModernLanguageDto selectedModernLanguage) {
        this.selectedModernLanguage = selectedModernLanguage;
    }

    public void setFolderAdministrationBean(FolderAdministrationBean folderAdministrationBean) {
        this.folderAdministrationBean = folderAdministrationBean;
    }
}
